/*
this file has been pimped by:
	hreed
	acarlson
 */
 
// future todos:
// bubble notifications

// [ajc] 12/15/04 -- changes in v.05
// Transparency works, with no need to restart when settings are changed

// v.07 - fixed wildcard substitution to work with urlencoded unicode characters

using CustomUIControls;
using iTunesLib;
using System.Drawing;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System;

namespace iTunesBlogger {
	public class MainForm : System.Windows.Forms.Form {
								
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ContextMenu contextMenu;
		private System.Windows.Forms.Label label;
		private System.Windows.Forms.ListBox lbRecentlyPlayed;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuItem;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItemAbout;
		private System.Windows.Forms.MenuItem menuItemAbout2;
		private System.Windows.Forms.MenuItem menuItemExit;
		private System.Windows.Forms.MenuItem menuItemNext;
		private System.Windows.Forms.MenuItem menuItemPause;
		private System.Windows.Forms.MenuItem menuItemPlay;
		private System.Windows.Forms.MenuItem menuItemPrev;
		private System.Windows.Forms.MenuItem menuItemSettings;
		private System.Windows.Forms.MenuItem menuItemSettings2;
		private System.Windows.Forms.MenuItem menuItemStop;
		private System.Windows.Forms.NotifyIcon notifyIcon;
		private System.Windows.Forms.StatusBar statusBar;
		private System.Windows.Forms.StatusBarPanel shortcutPanel;
		private System.Windows.Forms.StatusBarPanel topPanel;
		private System.Windows.Forms.StatusBarPanel updatePanel;
		
		// holds the last artist, album, and title played-- used to decide if things have changed and we should update
		string lastArtist = "";
		string lastAlbum = "";
		string lastTitle = "";

		// holds text which will be pasted to the clipboard if the user performs the shortcut
		string shortcutText = "";
	
		// the COM interface to iTunes
		iTunesApp iTunesInstance; 
	
		// our custom config class, which is serialized to XML when the program is closed
		private iTunesBloggerConfig config = new iTunesBloggerConfig();
		
		//TaskbarNotifier taskbarNotifier;
		
		#region Shortcut keys properties
		//import DLL for shortcut keys
		[DllImport("user32.dll")]
		public static extern bool RegisterHotKey(IntPtr hWnd, int id,int fsModifiers,int vlc);
		[DllImport("user32.dll")]
		public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
		
		public MainForm() {
			
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			
			InitializeComponent();
			
			iTunesInstance = new iTunesAppClass(); 
			
			// deserialize the XML config file
			XmlSerializer x = new XmlSerializer( typeof(iTunesBloggerConfig) );
			
			// if it exists, deserialize XML config file
			FileStream fs = null;
			try {
				fs = new FileStream( "iTunesBloggerConfig.xml", FileMode.Open );
			
				XmlReader reader = new XmlTextReader(fs);         
				config = (iTunesBloggerConfig) x.Deserialize( reader );
			}
			catch( FileNotFoundException ) {
				// ignore -- config file will be written
			}
			finally {
				if ( fs != null ) 
					fs.Close();
			}
			
			// Register the hot key shortcut
			if (config.shortcutState) {
				int systemkeyInt =  GetSystemKeyInteger(config.alt,
					config.ctrl,
					config.shift);
				int letterInt = GetLetterKeyInteger(config.letter);
				MainForm.RegisterHotKey(this.Handle, this.GetType().GetHashCode(),systemkeyInt, letterInt);
			}
			
			// do fancy stuff
			UpdateStatusText(config.shortcutState,
				config.performUpdate,
				"");
			
			iTunesInstance.OnPlayerPlayEvent += new iTunesLib._IiTunesEvents_OnPlayerPlayEventEventHandler(ITunesPlayEventHandler);	
			
			if (config.isOpacityEnabled)
				this.Opacity = config.opacityValue / 10;
			else 
				this.Opacity = 1;
	
			//UpdateTaskbarNotifier("Destr0y");
		}
		
		[STAThread]
		public static void Main(string[] args) {
			Application.Run(new MainForm());
		}
	
		// picks up events for when the user presses the shortcut keys
		protected override void WndProc(ref Message m) {
			if (m.Msg == 0x0312)
				Clipboard.SetDataObject(shortcutText);
				
			base.WndProc(ref m);
		}
		
		/// <summary>
		/// Calculates the int value of the system key combo
		/// </summary>
		/// <param name="alt">alt boolean</param>
		/// <param name="ctrl">ctrl boolean</param>
		/// <param name="shift">shift boolean</param>
		/// <returns>int value based on the Alt = 1, Ctrl = 2, Shift = 4</returns>
		private int GetSystemKeyInteger(bool alt, bool ctrl,bool shift){
			int x = 0;
			// Alt = 1, Ctrl = 2, Shift = 4
			if (alt) x+=1;
			if (ctrl) x+=2;
			if (shift) x+=4;
			return x;
		}
		
		/// <summary>
		/// Calculates the int value of the letter key 
		/// </summary>
		/// <param name="letters">string letter</param>
		/// <returns>int value based char</returns>
		private int GetLetterKeyInteger(string letters){
			if (letters.Length >0 ){
				char letter = letters[0];
				return (int)letter;
			}else
				return (int)'J';
		}

		/// <summary>
		/// inserts variables into a pattern 
		/// </summary>
		/// <param name="pattern">the string pattern</param>
		/// <param name="songArtist">the string pattern</param>
		/// <param name="songTitle">the string pattern</param>
		/// <param name="songAlbum">the string pattern</param>
		/// <param name="songGenre">the string pattern</param>
		/// <param name="songTime">the string pattern</param>
		/// <returns>string with everything inserted nicely</returns>
		private string ReplaceWildcardsWithSongDetails(string pattern, 
			string songArtist, 
			string songTitle,
			string songAlbum, 
			string songGenre, 
			string songTime, 
			string songYear,
			string songTrackNumber,
			string songDiscNumber,
			string songComment,
			string songKind,
			string songRating
			){
			string transform = pattern;
			transform = transform.Replace("%","%%%%"); // do this so we do not mix up extended url encoded chars for things we should replace!
			transform = transform.Replace("%%%%a",songArtist);
			transform = transform.Replace("%%%%t",songTitle);
			transform = transform.Replace("%%%%b",songAlbum);
			transform = transform.Replace("%%%%g",songGenre);
			transform = transform.Replace("%%%%y",songTime);
			transform = transform.Replace("%%%%d",songYear);
			transform = transform.Replace("%%%%n",songTrackNumber);
			transform = transform.Replace("%%%%p",songDiscNumber);
			transform = transform.Replace("%%%%c",songComment);
			transform = transform.Replace("%%%%k",songKind);
			transform = transform.Replace("%%%%r",songRating);
			return transform;
		}
		#endregion		
		
		private void UpdateStatusText(bool shortcutEnabled, bool webUpdateEnabled, string currentTrackTitle){
			string tooltipText = "iTunesBlogger";
			
			// the track name makes the tooltip too long, and make us crash!
			//tooltipText += System.Environment.NewLine + "Track: " + currentTrackTitle;
			
			if (shortcutEnabled) {
				string shortcutKeyLegend = "";
				if (config.shift) shortcutKeyLegend += "Shift-";
				if (config.ctrl) shortcutKeyLegend += "Ctrl-";
				if (config.alt) shortcutKeyLegend += "Alt-";
				shortcutKeyLegend += config.letter;
				shortcutPanel.Text = "HotKey: " + shortcutKeyLegend;
				tooltipText += System.Environment.NewLine + "  HotKey: " + shortcutKeyLegend;
			} else {
				shortcutPanel.Text = "HotKey: None";
				tooltipText += System.Environment.NewLine + "  HotKey: None";
			}
			
			if (webUpdateEnabled) {
				updatePanel.Text = "Web Update: On";
				tooltipText += System.Environment.NewLine + "  Web Update: On";
			} else {
				updatePanel.Text = "Web Update: Off";
				tooltipText += System.Environment.NewLine + "  Web Update: Off";
			}

			notifyIcon.Text = tooltipText;
			
			topPanel.Text = "Songs Played: " + lbRecentlyPlayed.Items.Count;
		}
		
		// not used right now -- harper's dev code
		private void UpdateTaskbarNotifier(string NotifyInfo){
			// i am pretty sure there is a memory leak somewhere. eep 
			TaskbarNotifier taskbarNotifier = new TaskbarNotifier();
			try {
				taskbarNotifier.SetBackgroundBitmap("skin.bmp",Color.FromArgb(255,0,255));
				taskbarNotifier.SetCloseBitmap("close.bmp",Color.FromArgb(255,0,255),new Point(127,8));
				taskbarNotifier.TitleRectangle=new Rectangle(40,9,70,25);
				taskbarNotifier.ContentRectangle=new Rectangle(8,41,133,68);
				taskbarNotifier.Show("TitleText",NotifyInfo,5,1500,500);
				
				taskbarNotifier.TitleText = "Title";
			} catch (Exception ex){
				MessageBox.Show (""+ex, "{0} Exception caught", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
			}
			taskbarNotifier.Dispose();
		}
		
		// called whenever iTunes plays a track
		private  void ITunesPlayEventHandler(object o) {
			
			// is the "web update" checkbox checked?
			bool performUpdate = config.performUpdate;
						
			// get properties for the new track being played
			IITTrack track = (IITTrack)o;
			string songArtist = track.Artist;
			string songTitle = track.Name;
			string songAlbum = track.Album;
			string songGenre = track.Genre;
			string songTime = track.Time;
			string songYear = "" + track.Year;
			string songTrackNumber = "" + track.TrackNumber;
			string songDiscNumber = "" + track.DiscNumber;
			string songComment = track.Comment;
			string songKind = track.KindAsString;
			string songRating = "" + track.Rating;
			
			// set the text which will be pasted to the clipboard if the user performs
			// the shortcut
			shortcutText = ReplaceWildcardsWithSongDetails(config.trackText,
				songArtist,
				songTitle,
				songAlbum, 
				songGenre,
				songTime,
				songYear,
				songTrackNumber,
				songDiscNumber,
				songComment,
				songKind,
				songRating);
			
			// check to see if we should update based on what needs to have changed, 
			// according to user settings
			if ("artist" == config.updateType) {
				if (songArtist == lastArtist)
					performUpdate = false;
				else
					lastArtist = songArtist;
			} else if ("album" == config.updateType) {
				if (songAlbum == lastAlbum)
					performUpdate = false;
				else
					lastAlbum = songAlbum;
			} else if ("song" == config.updateType) {
				if (songTitle == lastTitle)
					performUpdate = false;
				else
					lastTitle = songTitle;
			}
			
			if (performUpdate) {
				// construct the update url
				string update_url = config.url;
				update_url = ReplaceWildcardsWithSongDetails(config.url,
					System.Web.HttpUtility.UrlEncode(songArtist),
					System.Web.HttpUtility.UrlEncode(songTitle),
					System.Web.HttpUtility.UrlEncode(songAlbum), 
					System.Web.HttpUtility.UrlEncode(songGenre),
					System.Web.HttpUtility.UrlEncode(songTime),
					System.Web.HttpUtility.UrlEncode(songYear),
					System.Web.HttpUtility.UrlEncode(songTrackNumber),
					System.Web.HttpUtility.UrlEncode(songDiscNumber),
					System.Web.HttpUtility.UrlEncode(songComment),
					System.Web.HttpUtility.UrlEncode(songKind),
					System.Web.HttpUtility.UrlEncode(songRating));
				
				WebRequest wr = WebRequest.Create(update_url);
				WebResponse r = wr.GetResponse(); // we don't care about the actual response
				r.Close();
				// update the "recently played" list, and the system tray icon
				string fullTrackTitle = songArtist + " - " + songTitle ;
				lbRecentlyPlayed.Items.Insert(0,fullTrackTitle + " (Updated)");
				UpdateStatusText(config.shortcutState,
					config.performUpdate,
					fullTrackTitle);
				//UpdateTaskbarNotifier(shortcutText);
			} else {
				string fullTrackTitle = songArtist + " - "+ songTitle;
				lbRecentlyPlayed.Items.Insert(0,fullTrackTitle);
				UpdateStatusText(config.shortcutState,
					config.performUpdate,
					fullTrackTitle);
				//UpdateTaskbarNotifier(shortcutText);
			}
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.lbRecentlyPlayed = new System.Windows.Forms.ListBox();
			this.topPanel = new System.Windows.Forms.StatusBarPanel();
			this.contextMenu = new System.Windows.Forms.ContextMenu();
			this.menuItemSettings = new System.Windows.Forms.MenuItem();
			this.statusBar = new System.Windows.Forms.StatusBar();
			this.menuItemPrev = new System.Windows.Forms.MenuItem();
			this.menuItemAbout2 = new System.Windows.Forms.MenuItem();
			this.menuItemSettings2 = new System.Windows.Forms.MenuItem();
			this.menuItemExit = new System.Windows.Forms.MenuItem();
			this.updatePanel = new System.Windows.Forms.StatusBarPanel();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItemNext = new System.Windows.Forms.MenuItem();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuItemStop = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem = new System.Windows.Forms.MenuItem();
			this.shortcutPanel = new System.Windows.Forms.StatusBarPanel();
			this.menuItemPause = new System.Windows.Forms.MenuItem();
			this.menuItemPlay = new System.Windows.Forms.MenuItem();
			this.menuItemAbout = new System.Windows.Forms.MenuItem();
			this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
			this.label = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.topPanel)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.updatePanel)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.shortcutPanel)).BeginInit();
			this.SuspendLayout();
			// 
			// lbRecentlyPlayed
			// 
			this.lbRecentlyPlayed.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lbRecentlyPlayed.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.lbRecentlyPlayed.ForeColor = System.Drawing.SystemColors.HotTrack;
			this.lbRecentlyPlayed.HorizontalScrollbar = true;
			this.lbRecentlyPlayed.Location = new System.Drawing.Point(8, 24);
			this.lbRecentlyPlayed.Name = "lbRecentlyPlayed";
			this.lbRecentlyPlayed.Size = new System.Drawing.Size(432, 134);
			this.lbRecentlyPlayed.TabIndex = 1;
			// 
			// topPanel
			// 
			this.topPanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.topPanel.Text = "iTunesBlogger";
			this.topPanel.ToolTipText = "iTunesBlogger";
			this.topPanel.Width = 87;
			// 
			// contextMenu
			// 
			this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						this.menuItemSettings2,
																						this.menuItemAbout2,
																						this.menuItemExit,
																						this.menuItem});
			// 
			// menuItemSettings
			// 
			this.menuItemSettings.Index = 0;
			this.menuItemSettings.Text = "Settings";
			this.menuItemSettings.Click += new System.EventHandler(this.MenuItemSettingsClick);
			// 
			// statusBar
			// 
			this.statusBar.ContextMenu = this.contextMenu;
			this.statusBar.Location = new System.Drawing.Point(0, 169);
			this.statusBar.Name = "statusBar";
			this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						 this.topPanel,
																						 this.shortcutPanel,
																						 this.updatePanel});
			this.statusBar.ShowPanels = true;
			this.statusBar.Size = new System.Drawing.Size(448, 22);
			this.statusBar.TabIndex = 7;
			this.statusBar.Text = "iTunesBlogger";
			// 
			// menuItemPrev
			// 
			this.menuItemPrev.Index = 4;
			this.menuItemPrev.Text = "Prev";
			this.menuItemPrev.Click += new System.EventHandler(this.MenuItemPrevClick);
			// 
			// menuItemAbout2
			// 
			this.menuItemAbout2.Index = 1;
			this.menuItemAbout2.Text = "About";
			this.menuItemAbout2.Click += new System.EventHandler(this.MenuItemAboutClick);
			// 
			// menuItemSettings2
			// 
			this.menuItemSettings2.Index = 0;
			this.menuItemSettings2.Text = "Settings";
			this.menuItemSettings2.Click += new System.EventHandler(this.MenuItemSettingsClick);
			// 
			// menuItemExit
			// 
			this.menuItemExit.Index = 2;
			this.menuItemExit.Text = "Exit";
			this.menuItemExit.Click += new System.EventHandler(this.MenuItemExitClick);
			// 
			// updatePanel
			// 
			this.updatePanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
			this.updatePanel.Text = "Web Update";
			this.updatePanel.ToolTipText = "Web Update Status";
			this.updatePanel.Width = 292;
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemAbout});
			this.menuItem5.Text = "Help";
			// 
			// menuItemNext
			// 
			this.menuItemNext.Index = 3;
			this.menuItemNext.Text = "Next";
			this.menuItemNext.Click += new System.EventHandler(this.MenuItemNextClick);
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItem3,
																					 this.menuItem5});
			// 
			// menuItemStop
			// 
			this.menuItemStop.Index = 1;
			this.menuItemStop.Text = "Stop";
			this.menuItemStop.Click += new System.EventHandler(this.MenuItemStopClick);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemSettings});
			this.menuItem3.Text = "Options";
			// 
			// menuItem
			// 
			this.menuItem.Index = 3;
			this.menuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItemPlay,
																					 this.menuItemStop,
																					 this.menuItemPause,
																					 this.menuItemNext,
																					 this.menuItemPrev});
			this.menuItem.Text = "Control";
			// 
			// shortcutPanel
			// 
			this.shortcutPanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.shortcutPanel.Text = "Hotkey:";
			this.shortcutPanel.ToolTipText = "Hotkey Status";
			this.shortcutPanel.Width = 53;
			// 
			// menuItemPause
			// 
			this.menuItemPause.Index = 2;
			this.menuItemPause.Text = "Pause";
			this.menuItemPause.Click += new System.EventHandler(this.MenuItemPauseClick);
			// 
			// menuItemPlay
			// 
			this.menuItemPlay.Index = 0;
			this.menuItemPlay.Text = "Play";
			this.menuItemPlay.Click += new System.EventHandler(this.MenuItemPlayClick);
			// 
			// menuItemAbout
			// 
			this.menuItemAbout.Index = 0;
			this.menuItemAbout.Text = "About";
			this.menuItemAbout.Click += new System.EventHandler(this.MenuItemAboutClick);
			// 
			// notifyIcon
			// 
			this.notifyIcon.ContextMenu = this.contextMenu;
			this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
			this.notifyIcon.Text = "iTunes Blog Updater";
			this.notifyIcon.Visible = true;
			this.notifyIcon.DoubleClick += new System.EventHandler(this.NotifyIconDoubleClick);
			// 
			// label
			// 
			this.label.Location = new System.Drawing.Point(8, 8);
			this.label.Name = "label";
			this.label.Size = new System.Drawing.Size(176, 16);
			this.label.TabIndex = 6;
			this.label.Text = "Recently Played Tracks";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(448, 191);
			this.ContextMenu = this.contextMenu;
			this.Controls.Add(this.statusBar);
			this.Controls.Add(this.label);
			this.Controls.Add(this.lbRecentlyPlayed);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Menu = this.mainMenu;
			this.MinimumSize = new System.Drawing.Size(340, 220);
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "iTunesBlogger";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.MainFormClosing);
			((System.ComponentModel.ISupportInitialize)(this.topPanel)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.updatePanel)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.shortcutPanel)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		void MainFormClosing(object sender, System.ComponentModel.CancelEventArgs e) {
			XmlSerializer x = new XmlSerializer( typeof(iTunesBloggerConfig) );
			TextWriter writer = new StreamWriter( "iTunesBloggerConfig.xml" );
			x.Serialize( writer, this.config);	
			
			notifyIcon.Visible = false;
			
			if (config.shortcutState)
				MainForm.UnregisterHotKey(this.Handle, this.GetType().GetHashCode());
			
			System.Runtime.InteropServices.Marshal.ReleaseComObject(iTunesInstance);
		}
		
		// this is called when the window is minimized.  it lets us hide the window from the
		// taskbar.
		protected override void OnResize(EventArgs e) {
			if (this.WindowState == FormWindowState.Minimized)
				this.Hide();
			
			base.OnResize(e);
		}

		// when the system tray icon is double clicked, we display the window again, and let
		// it show up in the taskbar.
		void NotifyIconDoubleClick(object sender, System.EventArgs e) {
			if(this.WindowState != FormWindowState.Normal){
				this.Show();
				this.WindowState = FormWindowState.Normal;
			}else {
				this.Hide();
				this.WindowState = FormWindowState.Minimized;
				
			}
		}

		void MenuItemSettingsClick(object sender, System.EventArgs e) {
			// if this was called from the system tray, we need to make sure
			// the window is visible
			if (this.WindowState != FormWindowState.Normal) {
				this.Show();
				this.WindowState = FormWindowState.Normal;
			}

			bool originalShortcutEnabledSetting = config.shortcutState;
			
			//Display frmConfig as a modal dialog
			ConfigurationForm frmConfig = new ConfigurationForm(config);
			
			// show the modal dialog until the AcceptButton (OK) or CancelButton(Cancel) is pressed
			if (frmConfig.ShowDialog() == DialogResult.OK) {
				// extract the data from the dialog
				config = frmConfig.BloggerConfig;
				UpdateStatusText(config.shortcutState,
					config.performUpdate,
					"");

				if (config.shortcutState == true) {
					// unregister the previous hotkey, if one was enabled
					if (originalShortcutEnabledSetting == true) {
						MainForm.UnregisterHotKey(this.Handle, this.GetType().GetHashCode());
					}
					
					int systemKeyInt =  GetSystemKeyInteger(config.alt,
						config.ctrl,
						config.shift);
					int letterInt = GetLetterKeyInteger(config.letter);
					MainForm.RegisterHotKey(this.Handle, this.GetType().GetHashCode(), systemKeyInt, letterInt);
				} else if (originalShortcutEnabledSetting == true && config.shortcutState == false) {
					MainForm.UnregisterHotKey(this.Handle, this.GetType().GetHashCode());
				}
				
				if(config.isOpacityEnabled)
					this.Opacity = config.opacityValue / 10;
				else 
					this.Opacity = 1;
			}
		}
		
		void MenuItemAboutClick(object sender, System.EventArgs e) {
			// if this was called from the system tray, we need to make sure
			// the window is visible
			if (this.WindowState != FormWindowState.Normal) {
				this.Show();
				this.WindowState = FormWindowState.Normal;
			}
			AboutDialog frmAbout = new AboutDialog();
			
			frmAbout.ShowDialog();
		}
		
		void MenuItemExitClick(object sender, System.EventArgs e) {
			this.Close();			
		}
		
		void MenuItemPlayClick(object sender, System.EventArgs e) {
			iTunesInstance.Play();
		}
		
		void MenuItemPauseClick(object sender, System.EventArgs e) {
			iTunesInstance.Pause();
		}
		
		void MenuItemStopClick(object sender, System.EventArgs e) {
			iTunesInstance.Stop();
		}
		
		void MenuItemNextClick(object sender, System.EventArgs e) {
			iTunesInstance.NextTrack();
		}
		
		void MenuItemPrevClick(object sender, System.EventArgs e) {
			iTunesInstance.PreviousTrack();
		}
	}
}
